package hibernate;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import hibernate.domain.BankAccount;
import hibernate.domain.BillingDetails;
import hibernate.domain.CreditCard;
import hibernate.domain.Order;
import hibernate.domain.Product;
import hibernate.domain.dao.HibernateUtil;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.dialect.SQLServerDialect;

public class HibernateMain {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSession().openSession();
//		EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld");
//		
//		EntityManager em = emf.createEntityManager();
		Transaction tx = session.beginTransaction();
		
		try {
			tx.begin();
			saveBilling(session);
			tx.commit();
		} catch(Exception e) {
			e.printStackTrace();
			tx.rollback();
		} finally {
			/*
			Query q = session.createQuery("from hibernate.domain.Product");
			List<Product> l = (List<Product>)q.list();
			System.out.println(l.size());
			*/
			session.close();
				
		}
		
	}

	private static void saveBilling(Session session) {
		//			Query q = session.createQuery("from hibernate.domain.BillingDetails bd WHERE bd.id=3");
		//			BillingDetails bd = (BillingDetails)q.list().get(0);
					BankAccount ba = new BankAccount();
					ba.setAccount("account");
					ba.setId(4);
					session.saveOrUpdate(ba);
					
					//					CreditCard cc = new CreditCard();
		//			cc.setNumber("number");
		//			cc.setOwner("owner");
		//			session.save(cc);
	}

	private static void saveProduct(Session session) {
		Product p = new Product();
		
		p.setName("p098");
		p.setValue(12d);
		
		Order o = new Order();
		o.setQuantity(3);
		o.setCreatedOn(new Date());
		p.addOrder(o);
		
		o.setProduct(p);
		/*
		session.save(p);
		/*
		Map pMap = new HashMap();
		pMap.put("name", "p_map");
		pMap.put("value", 13d);
		session.save(Product.class.getName(), pMap);
		*/
		
//			session.persist(p);
//			session.persist(o)
		List<Product> l = session.createCriteria(Product.class).setFetchMode("orders", FetchMode.SELECT).list();
//			Query q = session.createQuery("from hibernate.domain.Product p LEFT JOIN FETCH p.orders os WHERE os.id > 1");
//			List<Product> l = (List<Product>)q.list();
		int a = 3;
		Product p1  = new Product();
		p1.setType(a);
		Product p2  = new Product();
		p2.setType(a);
		System.out.println(l.get(0).getType() == 3);
		System.out.println(l.get(0).getType() == p1.getType());
		System.out.println(l.get(0).getType().equals(p1.getType()));
		System.out.println(p1.getType() == p2.getType());
		System.out.println(new Integer(3) == new Integer(3));

		/*
		Product p1 = l.get(l.size() - 1);
		Order o1 = new ArrayList<Order>(p1.getOrders()).get(0);
//			o1.getProduct().setName("p123586");
		o1.setQuantity(5);
		p1.setName("p87");

		
		/*Query q = session.createQuery("from hibernate.domain.Order o ");
		System.out.println(q.list().size());
		/*
		Order o2 = (Order) q.uniqueResult();
		o2.setQuantity(8);
		o2.getProduct().setName("abc");
		session.save(o2);
		*/
	}
}
